﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Windows.Forms
Imports Microsoft.Win32

<Serializable()> _
Public Class Settings
    '設定を保存するフィールド
    Private _mcid As String
    Private _mcpas As String
    Private _java As String
    Private _xms As String
    Private _xmx As String
    Private _ftbpas As String

    '設定のプロパティ
    Public Property mcid() As String
        Get
            Return _mcid
        End Get
        Set(ByVal Value As String)
            _mcid = Value
        End Set
    End Property
    Public Property mcpas() As String
        Get
            Return _mcpas
        End Get
        Set(ByVal Value As String)
            _mcpas = Value
        End Set
    End Property
    Public Property java() As Object
        Get
            Return _java
        End Get
        Set(ByVal Value As Object)
            _java = Value
        End Set
    End Property
    Public Property xms() As Object
        Get
            Return _xms
        End Get
        Set(ByVal Value As Object)
            _xms = Value
        End Set
    End Property
    Public Property xmx() As Object
        Get
            Return _xmx
        End Get
        Set(ByVal Value As Object)
            _xmx = Value
        End Set
    End Property
    Public Property ftbpas() As Object
        Get
            Return _ftbpas
        End Get
        Set(ByVal Value As Object)
            _ftbpas = Value
        End Set
    End Property

    'コンストラクタ
    Public Sub New()
        _mcid = ""
        _mcpas = ""
        _java = ""
        _xms = ""
        _xmx = ""
        _ftbpas = ""
    End Sub

    'Settingsクラスのただ一つのインスタンス
    <NonSerialized()> _
    Private Shared _instance As Settings
    <System.Xml.Serialization.XmlIgnore()> _
    Public Shared Property Instance() As Settings
        Get
            If _instance Is Nothing Then
                _instance = New Settings
            End If
            Return _instance
        End Get
        Set(ByVal Value As Settings)
            _instance = Value
        End Set
    End Property

    ''' <summary>
    ''' 設定をXMLファイルから読み込み復元する
    ''' </summary>
    Public Shared Sub LoadFromXmlFile()
        Dim p As String = GetSettingPath()

        Dim fs As New FileStream( _
            p, FileMode.Open, FileAccess.Read)
        Dim xs As New System.Xml.Serialization.XmlSerializer( _
            GetType(Settings))
        '読み込んで逆シリアル化する
        Dim obj As Object = xs.Deserialize(fs)
        fs.Close()

        Instance = CType(obj, Settings)
    End Sub

    ''' <summary>
    ''' 現在の設定をXMLファイルに保存する
    ''' </summary>
    Public Shared Sub SaveToXmlFile()
        Dim p As String = GetSettingPath()

        Dim fs As New FileStream( _
            p, FileMode.Create, FileAccess.Write)
        Dim xs As New System.Xml.Serialization.XmlSerializer( _
            GetType(Settings))
        'シリアル化して書き込む
        xs.Serialize(fs, Instance)
        fs.Close()
    End Sub

    Private Shared Function GetSettingPath() As String
        Dim p As String = Application.StartupPath & "\setting.config"
        Return p
    End Function
End Class


